//
//  SecondViewController.m
//  gee
//
//  Created by apple on 11-5-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SecondViewController.h"


@implementation SecondViewController
@synthesize test_btn;

- (void)viewDidLoad
{
    
    
    [super viewDidLoad];
}

-(IBAction)test_btn_click:(id)sender{
    [self test_block_async_request];
}



-(void)test_block_async_request{
    
    [ASIHTTPRequest setDefaultUserAgentString:@"GEE IPHONE IOS4 1.0"];
    [ASIHTTPRequest setDefaultTimeOutSeconds:2];
    
    NSURL *url = [NSURL URLWithString:@"http://192.168.2.118/geo.php"];
    
    
    __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"31" forKey:@"lat"];
    [request setPostValue:@"121"  forKey:@"long"];
    
    [request setCompletionBlock:^{
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
        
        //NSLog(@"%@",responseData);
        //NSLog(@"%@",responseString);
       
        
        NSArray *locs = [responseString objectFromJSONString];
//        NSLog(@"%@",locs);
//        NSLog(@"%@",[locs lastObject]); 
//        NSLog(@"%d",[locs count]);
//        NSLog(@"%@",[locs objectAtIndex:0]);
//        
        for(NSDictionary *dict in locs) {
            NSLog(@"%@",[dict objectForKey:@"address"]);
            NSLog(@"%@",[dict objectForKey:@"description"]);
            NSLog(@"%@",[dict objectForKey:@"uuid"]);
            NSLog(@"%@",[dict objectForKey:@"uid"]);
            NSLog(@"%@",[dict objectForKey:@"time"]);
            NSLog(@"%@",[dict objectForKey:@"yyyy"]);
            NSArray *lloc = [dict objectForKey:@"loc"];
            NSLog(@"%@",[lloc objectAtIndex:0]);
            NSLog(@"%@",[lloc objectAtIndex:1]);
            
            
            
            
            
            
        }
       
        
        //
//        NSDate *date =[NSDate dateWithTimeIntervalSince1970 :[dict objectForKey:@"time"]]];
//        
//         NSLog(@"%@",date);
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//        
//        [dateFormatter setDateFormat:@"yyyy年-MMM-ddd日,hh:mm:ss"];
//        
//        NSString *time_str = [dateFormatter stringFromDate:[NSDate date]];
//        
//        [dateFormatter release];
//        
//        NSLog(@"%@",time_str);

        
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"error data %@",error); 
    }];
    

    [request startAsynchronous];
    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    test_btn = nil;
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
    [test_btn release];
    [super dealloc];
}

@end
